const db = require('../config/db');

const AdminUser = {
  // 获取所有用户
  getAllUsers: async () => {
    const query = 'SELECT * FROM users';
    return new Promise((resolve, reject) => {
      db.query(query, (error, results) => {
        if (error) return reject(error);
        resolve(results);
      });
    });
  },



  // 更新用户（管理员操作）
  updateUser: async (id, updates) => {
    const fields = Object.keys(updates).map((key) => `${key} = ?`).join(', ');
    const values = [...Object.values(updates), id];
    const query = `UPDATE users SET ${fields} WHERE user_id = ?`;

    return new Promise((resolve, reject) => {
      db.query(query, values, (error, results) => {
        if (error) return reject(error);
        resolve(results);
      });
    });
  },

  // 删除用户（管理员操作）
  deleteUser: async (id) => {
    const query = 'DELETE FROM users WHERE user_id = ?';
    return new Promise((resolve, reject) => {
      db.query(query, [id], (error, results) => {
        if (error) return reject(error);
        resolve(results);
      });
    });
  },
};

module.exports = AdminUser;
